<script>
    exports.install = function (Vue, options) {
        //浮点型乘法计算精度问题解决
        Vue.prototype.NumberMul = function (arg1, arg2) {
            let m = 0;
            let s1 = arg1 + '';
            let s2 = arg2 + '';
            try {
                m += s1.split(".")[1].length;
            } catch (e) {
            }
            try {
                m += s2.split(".")[1].length;
            } catch (e) {
            }
            return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
        };
        //浮点型加法：
        Vue.prototype.NumberAdd = function (arg1, arg2) {
            let r1, r2, m, n;
            try {
                r1 = (arg1+'').split(".")[1].length
            } catch (e) {
                r1 = 0
            }
            try {
                r2 = (arg2+'').split(".")[1].length
            } catch (e) {
                r2 = 0
            }
            m = Math.pow(10, Math.max(r1, r2))
            n = (r1 >= r2) ? r1 : r2;
            return Number(((arg1 * m + arg2 * m) / m).toFixed(n));
        };
        //Vue浮点型减法
        Vue.prototype.NumberSub = function (arg1, arg2) {
            let re1, re2, m, n;
            try {
                re1 = (arg1+'').split(".")[1].length;
            } catch (e) {
                re1 = 0;
            }
            try {
                re2 = (arg2+'').split(".")[1].length;
            } catch (e) {
                re2 = 0;
            }
            m = Math.pow(10, Math.max(re1, re2));
            n = (re1 >= re2) ? re1 : re2;
            return Number(((arg1 * m - arg2 * m) / m).toFixed(n));
        };
        //浮点型除法    除数，被除数， 保留的小数点后的位数
        Vue.prototype.NumberDiv = function (arg1, arg2, digit) {
            let t1 = 0, t2 = 0, r1, r2;
            let s1 = arg1 + '';
            let s2 = arg2 + '';
            try {
                t1 = s1.split(".")[1].length
            } catch (e) {
            }
            try {
                t2 = s2.split(".")[1].length
            } catch (e) {
            }
            r1 = Number(s1.replace(".", ""))
            r2 = Number(s2.replace(".", ""))
            //获取小数点后的计算值
            let result = ((r1 / r2) * Math.pow(10, t2 - t1))+''
			if (result.indexOf(".")!=-1) {
				let result2 = result.split(".")[1];
				result2 = result2.substring(0, digit > result2.length ? result2.length : digit);
				return Number(result.split(".")[0] + "." + result2);
			} else {
        return Number(result);
      }
        };

      //获取编码详情
      Vue.prototype.gDic = function (url, callback) {
        this.$http.get(url).then((res) => {
          callback(res.data.data);
        })
      };

      // table 计算合计
      Vue.prototype.gAmountTo = function ({columns, data}, sumColumns) {

        let nData = [...data];
        if (nData.length !== 0) {
          nData = nData.splice(1);
        }
        const sums = {};
        columns.forEach((column, index) => {
          const key = column.key;
          // if (index === 0) {
          if (column.title == '序号') {
            sums[key] = {
              key,
              value: '合计'
            };
            return;
          }
          let isSum = false;
          for (const item of sumColumns) {
            if (column.key === item) {
              isSum = true;
              break;
            }
          }
          if (isSum) {
            const values = nData.map(item => {
              return Number(item[key]) || 0
            });
            if (!values.every(value => isNaN(value))) {
              const v = values.reduce((prev, curr) => {
                const value = Number(curr);
                if (!isNaN(value)) {
                  return this.NumberAdd(prev, curr);
                } else {
                  return prev;
                }
              }, 0);
              sums[key] = {
                key,
                value: v
              };
            } else {
              sums[key] = {
                key,
                value: ''
              };
            }
          } else {
            sums[key] = {key, value: ''};
          }
        });
        return sums;
      };

      Vue.prototype.gFun = function (p1, p2) {//全局函数1
        alert('全局带参函数执行成功' + ":  调用方式:this.函数名");
        alert(p1 + " : " + p2);
      };
      Vue.prototype.gTestfun = function () {//全局函数2
        alert('全局函数----无参函数    执行成功');
      };
    };
</script>

